package edu.cmu.cs.webapp.ioms.ui.wizards.order;

import org.eclipse.jface.wizard.Wizard;

import edu.cmu.cs.webapp.ioms.models.Client;
import edu.cmu.cs.webapp.ioms.models.OrderStatus;
import edu.cmu.cs.webapp.ioms.models.PurchaseOrder;
import edu.cmu.cs.webapp.ioms.models.impl.IOMSFactoryImpl;

/**
 * 
 * @author Christian
 */
public class OrderWizard extends Wizard {
	private OrderMainWizardPage _generalPage;
	private OrderItemWizardPage _itemPage;
	private OrderCustomerWizardPage _clientPage;
	private boolean _isUpdate;
	private PurchaseOrder _model;

	public OrderWizard(boolean update, PurchaseOrder model) {
		_isUpdate = update;
		this._model = model;
	}

	@Override
	public void addPages() {
		_generalPage = new OrderMainWizardPage("General Page");
		_itemPage = new OrderItemWizardPage("Card Page");
		_clientPage = new OrderCustomerWizardPage("Customer Page");

		addPage(_generalPage);
		addPage(_clientPage);

		if (!_isUpdate) {
			addPage(_itemPage);
		}

	}

	@Override
	public boolean performFinish() {
		if (_model == null) {
			_model = IOMSFactoryImpl.eINSTANCE.createPurchaseOrder();
		}

		_model.setOrder_date(_generalPage.getOrderDate());
		_model.setExpected_shipping_date(_generalPage.getShippingDate());
		_model.setShipping_address(_generalPage.getShippingAddress());
		_model.setShipping_city(_generalPage.getShippingCity());
		_model.setShipping_state(_generalPage.getShippingState());
		_model.setShipping_country(_generalPage.getShippingCountry());
		_model.setStatus(OrderStatus.get(_generalPage.getStatus()));

		Client client = _clientPage.getClient();
		_model.setClient(client);

		_model.getItems().addAll(this._itemPage.getItems());

		return true;
	}

	public PurchaseOrder getModel() {
		return this._model;
	}

	public boolean isUpdate() {
		return _isUpdate;
	}

	@Override
	public boolean canFinish() {
		if (!_isUpdate) {
			return this._generalPage.isPageComplete()
					&& this._clientPage.isPageComplete()
					&& this._itemPage.isPageComplete();
		} else {
			return this._generalPage.isPageComplete();
		}

	}

}
